xquery version "1.0-ml";

import module namespace plib = "http://porvino.com/lib" at "/porvino/lib/lib.xqy";

let $did := xdmp:get-request-field("did")
let $dbase := xdmp:get-request-field("dbase")
let $tag := xdmp:get-request-field("tag")

return
    let $docpath := fn:concat("fn:doc('",$did,"')",$dbase)
    let $doc := xdmp:eval($docpath)
    let $delete-tag :=
        fn:concat(
            "xdmp:node-delete(",
                $docpath,
                "/tags/tag[node()='",
                fn:normalize-space($tag),
                "']",
            ")"
        )


    return
    if (
        fn:exists(
            $doc/tags/tag[fn:normalize-space(text()) = fn:normalize-space($tag)]
        )
    )
    then
        let $t1 :=
                xdmp:eval($delete-tag)
            return
            fn:concat("deleted tag=", $tag)
    else
        "does not exist"
       
           
           
              
